package com.superman.supermarket.client.activity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.user.client.Window;
import com.google.inject.Inject;
import com.superman.mvpframe.client.ajax.PostManAsyncCallBack;
import com.superman.mvpframe.client.mvp.IPageView;
import com.superman.mvpframe.client.mvp.IPageViewPresenter;
import com.superman.mvpframe.client.utils.StringUtils;
import com.superman.mvpframe.client.utils.XJsonUtil;
import com.superman.mvpframe.client.widget.mescroll.UpAndDownCallBack;
import com.superman.supermarket.client.mvp.ShopAbstractActivity;
import com.superman.supermarket.client.utils.ActivityJumpUrl;
import com.superman.supermarket.client.view.store.StoreView;
import com.superman.supermarket.client.view.store.StoreView.StoreViewPresenter;

/**
 * 店铺
 * 
 * @author superman
 * @version 2018年8月2日下午5:08:48
 */
public class StoreActivity extends ShopAbstractActivity implements UpAndDownCallBack, StoreViewPresenter {

	private String cid;

	private String storeid;

	private Map<String, Object> storeMap;

	@Inject
	StoreView storeView;

	@Override
	public void getListDataByPage(int page) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (!StringUtils.isEmpty(cid))
			map.put("CATALOG_ID", cid);
		map.put("STORE_ID", storeid);
		map.put("PAGE_INDEX", page);
		postman.post("/store/skus", map, new PostManAsyncCallBack() {

			@Override
			public void onBusinessError(String statusCode) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onBusinessOK(String response) {
				storeView.getPullToRefreshView().endBySize(XJsonUtil.getList(response), XJsonUtil.getCount(response));
			}
		});

	}

	private void getStoreDetail() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("STORE_ID", storeid);
		postman.post("/store/detail", map, new PostManAsyncCallBack() {

			@Override
			public void onBusinessError(String statusCode) {

			}

			@Override
			public void onBusinessOK(String response) {
				storeMap = XJsonUtil.getBody(response);

				storeView.setSkuImages(XJsonUtil.getList((JSONArray) storeMap.get("IMAGES")));
				storeView.setStoreBaseInfo(storeMap);

				if (storeMap.get("COUPONS") != null) {
					parseStoreCoupons(XJsonUtil.getList((JSONArray) storeMap.get("COUPONS")));
				} else {
					storeView.removeCouponModule();
				}

				if (storeMap.get("CATALOGS") != null) {
					storeView.setCateList(XJsonUtil.getList((JSONArray) storeMap.get("CATALOGS")));
					storeView.getPullToRefreshView().initMeScroll(StoreActivity.this, storeView.getWrapperPanelAbsTop() + "px", "0px");
					storeView.selectFirstCate();
				} else {
					storeView.removeCateModule();
				}
			}
		});

	}

	@Override
	public IPageView getView() {
		return storeView;
	}

	@Override
	public IPageViewPresenter getViewPresenter() {
		return this;
	}

	@Override
	public void onActivity(EventBus eventBus) {
	}

	private void parseStoreCoupons(List<Map<String, Object>> coupons) {
		if (coupons.size() > 0) {
			Map<String, Object> map = coupons.get(0);
			String name = String.valueOf(map.get("NAME"));
			storeView.setCouponName(name);
		} else {
			storeView.removeCouponModule();
		}
	}

	@Override
	public void popToRefresh() {
		// TODO Auto-generated method stub
	}

	@Override
	public void popToRefresh(Object contextData) {
		// TODO Auto-generated method stub

	}

	@Override
	public void pushToRefresh() {
		storeid = Window.Location.getParameter("storeid");
		getStoreDetail();
	}

	@Override
	public void pushToStoreCouponListPage() {
		ActivityJumpUrl.pushToStoreCouponListPage(storeid);
	}

	@Override
	public void refreshSkuListByCateId(String cid) {
		this.cid = cid;
		storeView.getPullToRefreshView().clearListAndResetScroll();
	}

}
